package com.housekeeping.service.impl;

import com.housekeeping.dao.OrderDAO;
import com.housekeeping.dao.impl.OrderDAOImpl;
import com.housekeeping.model.Order;
import com.housekeeping.service.OrderService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 订单服务实现类
 */
public class OrderServiceImpl implements OrderService {
    
    private OrderDAO orderDAO = new OrderDAOImpl();

    @Override
    public boolean createOrder(Order order) {
        if (order == null) {
            return false;
        }
        order.setOrderNo(generateOrderNo());

        if (order.getStatus() == null) {
            order.setStatus(1);
        }
        
        return orderDAO.insert(order) > 0;
    }

    @Override
    public String generateOrderNo() {
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String timestamp = sdf.format(new Date());
        int random = (int) (Math.random() * 9000) + 1000;
        return "HK" + timestamp + random;
    }

    @Override
    public Order getOrderById(Integer orderId) {
        return orderDAO.findById(orderId);
    }

    @Override
    public List<Order> getAllOrdersWithDetails() {
        return orderDAO.findAllWithDetails();
    }

    @Override
    public List<Order> getOrdersByUserId(Integer userId) {
        return orderDAO.findByUserId(userId);
    }

    @Override
    public List<Order> getOrdersByStaffId(Integer staffId) {
        return orderDAO.findByStaffId(staffId);
    }

    @Override
    public List<Order> getOrdersByStatus(Integer status) {
        return orderDAO.findByStatus(status);
    }

    @Override
    public boolean updateOrder(Order order) {
        if (order == null || order.getOrderId() == null) {
            return false;
        }
        return orderDAO.update(order) > 0;
    }

    @Override
    public boolean updateOrderStatus(Integer orderId, Integer status) {
        if (orderId == null || status == null) {
            return false;
        }
        return orderDAO.updateStatus(orderId, status) > 0;
    }

    @Override
    public boolean assignStaff(Integer orderId, Integer staffId) {
        if (orderId == null || staffId == null) {
            return false;
        }
        return orderDAO.assignStaff(orderId, staffId) > 0;
    }

    @Override
    public boolean cancelOrder(Integer orderId) {
        return updateOrderStatus(orderId, 5); 
    }

    @Override
    public boolean completeOrder(Integer orderId) {
        return updateOrderStatus(orderId, 4); 
    }

    @Override
    public boolean deleteOrder(Integer orderId) {
        return orderDAO.delete(orderId) > 0;
    }

    @Override
    public long getOrderCount() {
        return orderDAO.count();
    }
    
    @Override
    public int getOrderCountByStaffId(Integer staffId) {
        if (staffId == null) {
            return 0;
        }
        return orderDAO.countByStaffId(staffId);
    }
    
    @Override
    public int getCompletedOrderCountByStaffId(Integer staffId) {
        if (staffId == null) {
            return 0;
        }
        return orderDAO.countByStaffIdAndStatus(staffId, 3); 
    }
    
    @Override
    public int getOngoingOrderCountByStaffId(Integer staffId) {
        if (staffId == null) {
            return 0;
        }
        
        return orderDAO.countByStaffIdAndStatus(staffId, 1) + orderDAO.countByStaffIdAndStatus(staffId, 2);
    }
} 